 <script src="https://api.map.baidu.com/api?type=webgl&v=1.0&ak=gaps7QT8wKLxWPAKHldsyBo4GNR7SGX6"></script> 
<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">商家信息</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home">首页</a>
          <a><cite>商家信息</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar businesstoolbar">
                搜索：
            <select id="business-search-key">
                <option value="">-请选择-</option>
                <option value="code">商家编码</option>
                <option value="name">商家名称</option>
                <option value="contact">联系人</option>
            </select>&emsp;
            <input id="business-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="business-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
        </div>

        <table class="layui-table" id="business-table" lay-filter="business-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="business-table-bar">
 {{#   if(admin.hasPerm("business:business:update") ){   }}
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
 <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="bussinessUser">帐号</a>
 {{#     } }} 
 {{#  if(admin.hasPerm("business:business:delete") ){   }}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
 {{#     } }} 
</script>


<script type="text/html" id="businesstoolbar">
     {{#  if(admin.hasPerm("business:business:save")){ }}
     <button id="business-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>商家开户</button>
     {{#     } }} 
 
</script>

<script type="text/html" id="business-status-bar">
   
     <input type="checkbox"  value="{{d.id}}"   lay-skin="switch" lay-filter="switchStatus" {{d.status==1?'checked':''}} title="状态">
</script>

<script type="text/html" id="businessType">
     {{#    if(d.type==1){ }}
     <button type="button" class="layui-btn layui-btn-normal layui-btn-xs layui-btn-radius">代理商</button>
     {{#     }else if(d.type==2){ }} 
     <button type="button" class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius">商家</button>
     {{#     } }}  
</script>



<script>
   var admin;
    layui.use(['form', 'table', 'util', 'config', 'admin', 'upload','laytpl','element' ], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
          admin = layui.admin;
        //上传
        var upload = layui.upload;
        let laytpl = layui.laytpl;
        let element = layui.element;
        
        //监听指定开关
        form.on('switch(switchStatus)', function(obj){
 
          layer.load(2);
          admin.req('business/business/update',JSON.stringify( {
              id: obj.elem.value,
              status: obj.elem.checked ? 1 : 0
          }), function (data) {
              layer.closeAll('loading');
              if (data.code == 0) {
                  layer.msg(data.msg, {icon: 1, time: 500});
              
              } else {
                  layer.msg(data.msg, {icon: 2, time: 500});
                  $(obj.elem).prop('checked', !obj.elem.checked);
              }
           //   table.reload('business-table', {});
          }, 'POST');
         
        });

        // 渲染表格
        table.render({
            elem: '#business-table',
            url: config.base_server + 'business/business/list',
            method: 'GET',
            id:"business-table",
            page: true,
            cols: [[
                {type: 'numbers'},
           
                 {field: 'code',   title: '商家编号'},
                 {field: 'name',   title: '商家名称'},
                 {field: 'contact',   title: '联系人'},
                 {field: 'phone',   title: '联系电话'},
                 {fixed: 'right', align: 'center', title: "商家类型"   ,  toolbar:"#businessType" },
                 {field: 'effectTime',   title: '有效期'},
                 {field: 'createTime',   title: '创建时间'},
                 {fixed: 'right',  align: 'center', title: '状态'  ,width: 81,toolbar: '#business-status-bar',},

                        
                {fixed: 'right', align: 'center', toolbar: '#business-table-bar', title: '操作', width: 175}
            ]]
        });
        
        laytpl($("#businesstoolbar")[0].innerHTML).render({ }, function(html){
			  $(".layui-card-body .businesstoolbar").append(html);
			  element.render('nav');
			  element.render('select');
		  });

        // 添加按钮点击事件
        $('#business-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(business-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
             
                showEditModel(data);
            }  else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('business/business/delete/'+obj.data.id, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('business-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'delete');
                });
            }else if(layEvent==='bussinessUser'){
            	showBusinessUser(data);
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            var user=config.getUser();
        	  var title =null;
        	  console.log(user);
            if(user.businesstype==0)
                title=  data ? '代理商编缉' : '代理商开户';
        	  if(user.businesstype==1)
                  title=  data ? '商家编缉' : '商家开户';
            //修改方法名
            if (data){
                data.method="update"
            }else{
            	data={method:"save"};
            }
 
            admin.putTempData('t_business', data);
            admin.popupCenter({
                title: title,
                area: "60%",
                path: 'pages/business/business_form.html',
                finish: function () {
                    table.reload('business-table', {});
                }
            });
        };
        
        var showBusinessUser=function(data){
        	 admin.putTempData('t_business', data);
        	   admin.popupCenter({
                   title: "商家帐号",
                   area: "80%",
                   path: 'pages/system/user.html',
                   finish: function () {
                       table.reload('business-table', {});
                   }
               });
        }

        // 搜索按钮点击事件
        $('#business-btn-search').click(function () {
            var key = $('#business-search-key').val();
            var value = $('#business-search-value').val();
            table.reload('business-table', {where: {searchKey: key, searchValue: value}});
        });

    });
</script>
